package bdhub.littletail.app.utils;

import android.util.Log;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by Freddy on 2016/2/23.
 * chenshichao@outlook.com
 * MD5文件名称生成工具
 */
public class Md5FileNameGenerator implements FileNameGenerator {
    private final String TAG = Md5FileNameGenerator.class.getSimpleName();

    private static final String HASH_ALGORITHM = "MD5";
    private static final int RADIX = 36;

    public Md5FileNameGenerator() {
    }

    @Override
    public String generate(String imageUri) {
        byte[] md5 = this.getMD5(imageUri.getBytes());
        BigInteger bi = (new BigInteger(md5)).abs();
        return bi.toString(36);
    }

    private byte[] getMD5(byte[] data) {
        byte[] hash = null;

        try {
            MessageDigest e = MessageDigest.getInstance("MD5");
            e.update(data);
            hash = e.digest();
        } catch (NoSuchAlgorithmException var4) {
            Log.e(TAG, var4.toString());
        }

        return hash;
    }
}
